package com.example.robotwatcher.backend.charts;

import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

public class mLineChart {
    private LineChart mLineChart;
    private LineData mLineData;         // 所有折线集合
    private LineDataSet mLineDataSet;   // 一条折线点集
    public int Index;

    public mLineChart(LineChart chart,int index)
    {
        // pass parameter
        mLineChart = chart;
        Index = index;

        //setting
        mLineChart.setDrawBorders(true);

        mLineChart.setTouchEnabled(true);
        mLineChart.setScaleEnabled(true);

        //Todo 自定义图标样式
        createLineChart();
    }

    public void createLineChart()
    {
        //设置数据
        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            entries.add(new Entry(i, (float) (Math.random()) * 80));
        }
        //一个LineDataSet就是一条线
        mLineDataSet = new LineDataSet(entries, "cpu占用率");
        mLineData = new LineData(mLineDataSet);
        mLineChart.setData(mLineData);
    }

    public void addEntry(float yValue)
    {
        int xCount = mLineData.getDataSetByIndex(Index).getEntryCount();

        Entry entry = new Entry(xCount,yValue);
        mLineData.addEntry(entry,Index);

        // 通知变更
        mLineData.notifyDataChanged();
        mLineChart.notifyDataSetChanged();

        Log.d("mLineChart","UpdateValue");

        mLineChart.setVisibleXRangeMaximum(15);
        mLineChart.moveViewToX(mLineData.getEntryCount() - 4);
        // TODO
        // 动态加载图表
//        lineChart.moveViewToAnimated(xCount-4,yValue, YAxis.AxisDependency.LEFT,1000);
    }

}
